using System.Reflection;
using FreeSql.DataAnnotations;
using KrasDD.Extensions;
using KrasDD.Models;
using KrasDD.Attributes;

namespace KrasDD.Data
{
    public class SeedDataInitializer
    {
        private readonly IFreeSql _db;

        public SeedDataInitializer(IFreeSql db)
        {
            _db = db;
        }

        public static Type[] GetTypesByTableAttribute()
        {
            List<Type> tableAssembies = new List<Type>();
            foreach (Type type in Assembly.GetAssembly(typeof(AppBaseEntity)).GetExportedTypes())
            foreach (Attribute attribute in type.GetCustomAttributes())
                if (attribute is TableAttribute tableAttribute)
                    if (tableAttribute.DisableSyncStructure == false)
                        tableAssembies.Add(type);

            return tableAssembies.ToArray();
        }

        public async Task InitializeAsync()
        {
            _db.CodeFirst.SyncStructure(GetTypesByTableAttribute());
            //查询是否有innovator.fn_CheckEntityAccess函数
            // var result = await _db.Ado.ExecuteScalarAsync(
            //     @"SELECT COUNT(*) FROM sys.objects WHERE object_id = OBJECT_ID(N'innovator.fn_CheckEntityAccess') AND type IN (N'FN', N'IF', N'TF', N'FS', N'FT')"
            // );
            //var exists = Convert.ToInt32(result);
            //if (exists == 0)
            //{
            //    var sql = File.ReadAllText("Scripts/fn_CheckEntityAccess.sql");
            //    await _db.Ado.ExecuteNonQueryAsync(sql);
            //}
            // 检查是否已经有数据，避免重复插入
            try
            {
                var existingItemTypeCount = await _db.Select<ITEMTYPE>()
                    .DisableGlobalFilter()
                    .CountAsync();
                if (existingItemTypeCount > 0)
                {
                    return; // 已有数据，跳过初始化
                }
            }
            catch { }
            
            // 在种子数据初始化期间禁用权限检查以避免循环依赖
            Console.WriteLine("开始初始化种子数据...");

            // 第一阶段：从属性创建ITEMTYPE记录
            var itemTypes = GetItemTypesFromAttributes();
            await _db.Insert<ITEMTYPE>().AppendData(itemTypes).ExecuteAffrowsAsync();

            // 获取所有创建的ITEMTYPE，用于后续关联
            var allItemTypes = await _db.Select<ITEMTYPE>().DisableGlobalFilter().ToListAsync();
            var itemTypeDict = allItemTypes.ToDictionary(x => x.NAME, x => x);

            // 第三阶段：为所有ITEMTYPE创建PROPERTY记录
            var properties = CreateProperties(itemTypeDict);
            await _db.Insert<PROPERTY>().AppendData(properties).ExecuteAffrowsAsync();

            // 第四阶段：为关系实体类创建RELATIONSHIPTYPE记录
            var relationshipTypes = CreateRelationshipTypes(itemTypeDict);
            await _db.Insert<RELATIONSHIPTYPE>()
                .AppendData(relationshipTypes)
                .ExecuteAffrowsAsync();

            // 第五阶段：创建特殊身份记录
            var specialIdentities = CreateSpecialIdentities();
            await _db.Insert<IDENTITY>().AppendData(specialIdentities).ExecuteAffrowsAsync();

            // 第六阶段：创建基础权限记录
            var basePermissions = CreateBasePermissions();
            await _db.Insert<PERMISSION>().AppendData(basePermissions).ExecuteAffrowsAsync();

            // 第七阶段：创建基础访问权限记录
            var baseAccess = CreateBaseAccess(itemTypeDict, basePermissions, specialIdentities);
            await _db.Insert<ACCESS>().AppendData(baseAccess).ExecuteAffrowsAsync();
            
            // 验证权限系统初始化是否成功
            await VerifyPermissionSystemInitialization();
        }
        
        /// <summary>
        /// 验证权限系统初始化是否成功
        /// </summary>
        private async Task VerifyPermissionSystemInitialization()
        {
            try
            {
                Console.WriteLine("验证权限系统初始化...");
                
                // 检查特殊身份是否创建成功
                var worldIdentity = await _db.Select<IDENTITY>()
                    .DisableGlobalFilter()
                    .Where(i => i.NAME == "World")
                    .FirstAsync();
                
                var creatorIdentity = await _db.Select<IDENTITY>()
                    .DisableGlobalFilter()
                    .Where(i => i.NAME == "Creator")
                    .FirstAsync();
                
                var ownerIdentity = await _db.Select<IDENTITY>()
                    .DisableGlobalFilter()
                    .Where(i => i.NAME == "Owner")
                    .FirstAsync();
                
                if (worldIdentity == null || creatorIdentity == null || ownerIdentity == null)
                {
                    Console.WriteLine("警告: 特殊身份创建不完整");
                    return;
                }
                
                // 检查基础权限是否创建成功
                var readPermission = await _db.Select<PERMISSION>()
                    .DisableGlobalFilter()
                    .Where(p => p.NAME == "默认读取权限")
                    .FirstAsync();
                
                var fullPermission = await _db.Select<PERMISSION>()
                    .DisableGlobalFilter()
                    .Where(p => p.NAME == "默认完全权限")
                    .FirstAsync();
                
                if (readPermission == null || fullPermission == null)
                {
                    Console.WriteLine("警告: 基础权限创建不完整");
                    return;
                }
                
                // 检查访问权限是否创建成功
                var accessCount = await _db.Select<ACCESS>()
                    .DisableGlobalFilter()
                    .CountAsync();
                
                if (accessCount < 3)
                {
                    Console.WriteLine("警告: 基础访问权限创建不完整");
                    return;
                }
                
                Console.WriteLine("权限系统初始化成功!");
                Console.WriteLine($"- World身份ID: {worldIdentity.ID}");
                Console.WriteLine($"- Creator身份ID: {creatorIdentity.ID}");
                Console.WriteLine($"- Owner身份ID: {ownerIdentity.ID}");
                Console.WriteLine($"- 读取权限ID: {readPermission.ID}");
                Console.WriteLine($"- 完全权限ID: {fullPermission.ID}");
                Console.WriteLine($"- 访问权限记录数: {accessCount}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"权限系统初始化验证失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 从ItemTypeAttribute创建ITEMTYPE记录
        /// </summary>
        private List<ITEMTYPE> GetItemTypesFromAttributes()
        {
            var itemTypes = new List<ITEMTYPE>();
            var entityTypes = Assembly.GetAssembly(typeof(AppBaseEntity))
                .GetExportedTypes()
                .Where(t => typeof(AppBaseEntity).IsAssignableFrom(t) && !t.IsAbstract);
            
            foreach (var entityType in entityTypes)
            {
                var attribute = entityType.GetCustomAttribute<ItemTypeAttribute>();
                if (attribute != null)
                {
                    // 根据名称生成标签
                    var label = GetLabelFromName(attribute.Name);
                    var labelPlural = GetLabelPluralFromName(attribute.Name);
                    
                    itemTypes.Add(new ITEMTYPE
                    {
                        ID = attribute.Id, // 使用属性中指定的固定ID
                        NAME = attribute.Name,
                        LABEL = label,
                        LABEL_PLURAL = labelPlural,
                        IS_RELATIONSHIP = attribute.IsRelationship,
                        IMPLEMENTATION_TYPE = "table",
                        STRUCTURE_VIEW = "tabs off",
                        REVISIONS = "7FE395DD8B9F4E1090756A34B733D75E",
                        SHOW_PARAMETERS_TAB = "1",
                        ALLOW_PRIVATE_PERMISSION = true,
                        ENFORCE_DISCOVERY = true,
                        CREATED_ON = DateTime.Now,
                        CONFIG_ID = GenerateId(),
                        CREATED_BY_ID = "00000000000000000000000000000000",
                    });
                }
            }
            return itemTypes;
        }
        
        /// <summary>
        /// 根据名称生成标签
        /// </summary>
        private string GetLabelFromName(string name)
        {
            // 简单的英译中映射
            var labelMap = new Dictionary<string, string>
            {
                { "USER", "用户" },
                { "TEAM", "团队" },
                { "IDENTITY", "身份" },
                { "PERMISSION", "权限" },
                { "DOCUMENT", "文档" },
                { "FILE", "文件" },
                { "METHOD", "方法" },
                { "TEAM_IDENTITY", "团队身份" },
                { "MEMBER", "成员" },
                { "ALIAS", "别名" },
                { "ACCESS", "访问权限" },
                { "DOCUMENT_FILE", "文档文件" },
                { "SERVER_EVENT", "服务器事件" }
            };
            
            return labelMap.TryGetValue(name, out var label) ? label : name;
        }
        
        /// <summary>
        /// 根据名称生成复数标签
        /// </summary>
        private string GetLabelPluralFromName(string name)
        {
            var label = GetLabelFromName(name);
            // 简单的复数处理：添加"s"
            return label + "s";
        }

        private List<ITEMTYPE> CreateNormalItemTypes()
        {
            return new List<ITEMTYPE>
            {
                new ITEMTYPE
                {
                    ID = GenerateId(),
                    NAME = "USER",
                    LABEL = "用户",
                    LABEL_PLURAL = "用户",
                    IS_RELATIONSHIP = false,
                    IMPLEMENTATION_TYPE = "table",
                    STRUCTURE_VIEW = "tabs off",
                    REVISIONS = "7FE395DD8B9F4E1090756A34B733D75E",
                    SHOW_PARAMETERS_TAB = "1",
                    ALLOW_PRIVATE_PERMISSION = true,
                    ENFORCE_DISCOVERY = true,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                },
                new ITEMTYPE
                {
                    ID = GenerateId(),
                    NAME = "TEAM",
                    LABEL = "团队",
                    LABEL_PLURAL = "团队",
                    IS_RELATIONSHIP = false,
                    IMPLEMENTATION_TYPE = "table",
                    STRUCTURE_VIEW = "tabs off",
                    REVISIONS = "7FE395DD8B9F4E1090756A34B733D75E",
                    SHOW_PARAMETERS_TAB = "1",
                    ALLOW_PRIVATE_PERMISSION = true,
                    ENFORCE_DISCOVERY = true,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                },
                new ITEMTYPE
                {
                    ID = GenerateId(),
                    NAME = "IDENTITY",
                    LABEL = "身份",
                    LABEL_PLURAL = "身份",
                    IS_RELATIONSHIP = false,
                    IMPLEMENTATION_TYPE = "table",
                    STRUCTURE_VIEW = "tabs off",
                    REVISIONS = "7FE395DD8B9F4E1090756A34B733D75E",
                    SHOW_PARAMETERS_TAB = "1",
                    ALLOW_PRIVATE_PERMISSION = true,
                    ENFORCE_DISCOVERY = true,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                },
                new ITEMTYPE
                {
                    ID = GenerateId(),
                    NAME = "PERMISSION",
                    LABEL = "权限",
                    LABEL_PLURAL = "权限",
                    IS_RELATIONSHIP = false,
                    IMPLEMENTATION_TYPE = "table",
                    STRUCTURE_VIEW = "tabs off",
                    REVISIONS = "7FE395DD8B9F4E1090756A34B733D75E",
                    SHOW_PARAMETERS_TAB = "1",
                    ALLOW_PRIVATE_PERMISSION = true,
                    ENFORCE_DISCOVERY = true,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                },
                new ITEMTYPE
                {
                    ID = GenerateId(),
                    NAME = "DOCUMENT",
                    LABEL = "文档",
                    LABEL_PLURAL = "文档",
                    IS_RELATIONSHIP = false,
                    IMPLEMENTATION_TYPE = "table",
                    STRUCTURE_VIEW = "tabs off",
                    REVISIONS = "7FE395DD8B9F4E1090756A34B733D75E",
                    SHOW_PARAMETERS_TAB = "1",
                    ALLOW_PRIVATE_PERMISSION = true,
                    ENFORCE_DISCOVERY = true,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                },
                new ITEMTYPE
                {
                    ID = GenerateId(),
                    NAME = "FILE",
                    LABEL = "文件",
                    LABEL_PLURAL = "文件",
                    IS_RELATIONSHIP = false,
                    IMPLEMENTATION_TYPE = "table",
                    STRUCTURE_VIEW = "tabs off",
                    REVISIONS = "7FE395DD8B9F4E1090756A34B733D75E",
                    SHOW_PARAMETERS_TAB = "1",
                    ALLOW_PRIVATE_PERMISSION = true,
                    ENFORCE_DISCOVERY = true,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                },
                new ITEMTYPE
                {
                    ID = GenerateId(),
                    NAME = "METHOD",
                    LABEL = "方法",
                    LABEL_PLURAL = "方法",
                    IS_RELATIONSHIP = false,
                    IMPLEMENTATION_TYPE = "table",
                    STRUCTURE_VIEW = "tabs off",
                    REVISIONS = "7FE395DD8B9F4E1090756A34B733D75E",
                    SHOW_PARAMETERS_TAB = "1",
                    ALLOW_PRIVATE_PERMISSION = true,
                    ENFORCE_DISCOVERY = true,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                },
            };
        }

        private List<ITEMTYPE> CreateRelationshipItemTypes()
        {
            return new List<ITEMTYPE>
            {
                new ITEMTYPE
                {
                    ID = GenerateId(),
                    NAME = "TEAM_IDENTITY",
                    LABEL = "团队身份",
                    LABEL_PLURAL = "团队身份",
                    IS_RELATIONSHIP = true,
                    IMPLEMENTATION_TYPE = "table",
                    STRUCTURE_VIEW = "tabs off",
                    REVISIONS = "7FE395DD8B9F4E1090756A34B733D75E",
                    SHOW_PARAMETERS_TAB = "1",
                    ALLOW_PRIVATE_PERMISSION = true,
                    ENFORCE_DISCOVERY = true,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                },
                new ITEMTYPE
                {
                    ID = GenerateId(),
                    NAME = "MEMBER",
                    LABEL = "成员",
                    LABEL_PLURAL = "成员",
                    IS_RELATIONSHIP = true,
                    IMPLEMENTATION_TYPE = "table",
                    STRUCTURE_VIEW = "tabs off",
                    REVISIONS = "7FE395DD8B9F4E1090756A34B733D75E",
                    SHOW_PARAMETERS_TAB = "1",
                    ALLOW_PRIVATE_PERMISSION = true,
                    ENFORCE_DISCOVERY = true,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                },
                new ITEMTYPE
                {
                    ID = GenerateId(),
                    NAME = "ALIAS",
                    LABEL = "别名",
                    LABEL_PLURAL = "别名",
                    IS_RELATIONSHIP = true,
                    IMPLEMENTATION_TYPE = "table",
                    STRUCTURE_VIEW = "tabs off",
                    REVISIONS = "7FE395DD8B9F4E1090756A34B733D75E",
                    SHOW_PARAMETERS_TAB = "1",
                    ALLOW_PRIVATE_PERMISSION = true,
                    ENFORCE_DISCOVERY = true,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                },
                new ITEMTYPE
                {
                    ID = GenerateId(),
                    NAME = "ACCESS",
                    LABEL = "访问权限",
                    LABEL_PLURAL = "访问权限",
                    IS_RELATIONSHIP = true,
                    IMPLEMENTATION_TYPE = "table",
                    STRUCTURE_VIEW = "tabs off",
                    REVISIONS = "7FE395DD8B9F4E1090756A34B733D75E",
                    SHOW_PARAMETERS_TAB = "1",
                    ALLOW_PRIVATE_PERMISSION = true,
                    ENFORCE_DISCOVERY = true,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                },
                new ITEMTYPE
                {
                    ID = GenerateId(),
                    NAME = "DOCUMENT_FILE",
                    LABEL = "文档文件",
                    LABEL_PLURAL = "文档文件",
                    IS_RELATIONSHIP = true,
                    IMPLEMENTATION_TYPE = "table",
                    STRUCTURE_VIEW = "tabs off",
                    REVISIONS = "7FE395DD8B9F4E1090756A34B733D75E",
                    SHOW_PARAMETERS_TAB = "1",
                    ALLOW_PRIVATE_PERMISSION = true,
                    ENFORCE_DISCOVERY = true,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                },
                new ITEMTYPE
                {
                    ID = GenerateId(),
                    NAME = "SERVER_EVENT",
                    LABEL = "服务器事件",
                    LABEL_PLURAL = "服务器事件",
                    IS_RELATIONSHIP = true,
                    IMPLEMENTATION_TYPE = "table",
                    STRUCTURE_VIEW = "tabs off",
                    REVISIONS = "7FE395DD8B9F4E1090756A34B733D75E",
                    SHOW_PARAMETERS_TAB = "1",
                    ALLOW_PRIVATE_PERMISSION = true,
                    ENFORCE_DISCOVERY = true,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                },
            };
        }

        private List<PROPERTY> CreateProperties(Dictionary<string, ITEMTYPE> itemTypeDict)
        {
            var properties = new List<PROPERTY>();

            // 为每个ITEMTYPE创建基础属性（来自AppBaseEntity）
            var baseProperties = new[]
            {
                new
                {
                    Name = "ID",
                    Label = "ID",
                    DataType = "string",
                    IsKeyed = true,
                },
                new
                {
                    Name = "CLASSIFICATION",
                    Label = "分类",
                    DataType = "string",
                    IsKeyed = false,
                },
                new
                {
                    Name = "CONFIG_ID",
                    Label = "配置ID",
                    DataType = "string",
                    IsKeyed = false,
                },
                new
                {
                    Name = "CREATED_BY_ID",
                    Label = "创建者ID",
                    DataType = "string",
                    IsKeyed = false,
                },
                new
                {
                    Name = "CREATED_ON",
                    Label = "创建时间",
                    DataType = "datetime",
                    IsKeyed = false,
                },
                new
                {
                    Name = "CURRENT_STATE",
                    Label = "当前状态",
                    DataType = "string",
                    IsKeyed = false,
                },
                new
                {
                    Name = "GENERATION",
                    Label = "代",
                    DataType = "integer",
                    IsKeyed = false,
                },
                new
                {
                    Name = "IS_CURRENT",
                    Label = "当前版本",
                    DataType = "boolean",
                    IsKeyed = false,
                },
                new
                {
                    Name = "IS_RELEASED",
                    Label = "已发布",
                    DataType = "boolean",
                    IsKeyed = false,
                },
                new
                {
                    Name = "KEYED_NAME",
                    Label = "键名",
                    DataType = "string",
                    IsKeyed = false,
                },
                new
                {
                    Name = "LOCKED_BY_ID",
                    Label = "锁定者ID",
                    DataType = "string",
                    IsKeyed = false,
                },
                new
                {
                    Name = "MAJOR_REV",
                    Label = "主版本",
                    DataType = "string",
                    IsKeyed = false,
                },
                new
                {
                    Name = "MANAGED_BY_ID",
                    Label = "管理者ID",
                    DataType = "string",
                    IsKeyed = false,
                },
                new
                {
                    Name = "MINOR_REV",
                    Label = "次版本",
                    DataType = "string",
                    IsKeyed = false,
                },
                new
                {
                    Name = "MODIFIED_BY_ID",
                    Label = "修改者ID",
                    DataType = "string",
                    IsKeyed = false,
                },
                new
                {
                    Name = "MODIFIED_ON",
                    Label = "修改时间",
                    DataType = "datetime",
                    IsKeyed = false,
                },
                new
                {
                    Name = "NEW_VERSION",
                    Label = "新版本",
                    DataType = "boolean",
                    IsKeyed = false,
                },
                new
                {
                    Name = "NOT_LOCKABLE",
                    Label = "不可锁定",
                    DataType = "boolean",
                    IsKeyed = false,
                },
                new
                {
                    Name = "OWNED_BY_ID",
                    Label = "所有者ID",
                    DataType = "string",
                    IsKeyed = false,
                },
                new
                {
                    Name = "PERMISSION_ID",
                    Label = "权限ID",
                    DataType = "string",
                    IsKeyed = false,
                },
                new
                {
                    Name = "STATE",
                    Label = "状态",
                    DataType = "string",
                    IsKeyed = false,
                },
                new
                {
                    Name = "TEAM_ID",
                    Label = "团队ID",
                    DataType = "string",
                    IsKeyed = false,
                },
            };

            foreach (var itemType in itemTypeDict.Values)
            {
                foreach (var baseProp in baseProperties)
                {
                    properties.Add(
                        new PROPERTY
                        {
                            ID = GenerateId(),
                            NAME = baseProp.Name,
                            LABEL = baseProp.Label,
                            DATA_TYPE = baseProp.DataType,
                            IS_KEYED = baseProp.IsKeyed,
                            SOURCE_ID = itemType.ID,
                            CREATED_ON = DateTime.Now,
                            CONFIG_ID = GenerateId(),
                            CREATED_BY_ID = "00000000000000000000000000000000",
                        }
                    );
                }
            }

            // 为关系实体类添加SOURCE_ID和RELATED_ID属性
            var relationshipTypes = new[]
            {
                "TEAM_IDENTITY",
                "MEMBER",
                "ALIAS",
                "ACCESS",
                "DOCUMENT_FILE",
                "SERVER_EVENT",
            };
            var relationshipProperties = new[]
            {
                new
                {
                    Name = "SOURCE_ID",
                    Label = "源ID",
                    DataType = "string",
                    IsKeyed = false,
                },
                new
                {
                    Name = "RELATED_ID",
                    Label = "相关ID",
                    DataType = "string",
                    IsKeyed = false,
                },
            };

            foreach (var relType in relationshipTypes)
            {
                if (itemTypeDict.TryGetValue(relType, out var itemType))
                {
                    foreach (var relProp in relationshipProperties)
                    {
                        properties.Add(
                            new PROPERTY
                            {
                                ID = GenerateId(),
                                NAME = relProp.Name,
                                LABEL = relProp.Label,
                                DATA_TYPE = relProp.DataType,
                                IS_KEYED = relProp.IsKeyed,
                                SOURCE_ID = itemType.ID,
                                CREATED_ON = DateTime.Now,
                                CONFIG_ID = GenerateId(),
                                CREATED_BY_ID = "00000000000000000000000000000000",
                            }
                        );
                    }
                }
            }

            // 为特定实体类添加特有属性
            AddSpecificProperties(properties, itemTypeDict);

            return properties;
        }

        private void AddSpecificProperties(
            List<PROPERTY> properties,
            Dictionary<string, ITEMTYPE> itemTypeDict
        )
        {
            // USER特有属性
            if (itemTypeDict.TryGetValue("USER", out var userType))
            {
                var userProps = new[]
                {
                    new
                    {
                        Name = "CELL",
                        Label = "手机",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "COMPANY_NAME",
                        Label = "公司名称",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "DEFAULT_VAULT",
                        Label = "默认仓库",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "EMAIL",
                        Label = "邮箱",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "ESIGNATURE",
                        Label = "电子签名",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "FAX",
                        Label = "传真",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "FIRST_NAME",
                        Label = "名",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "HOME_PHONE",
                        Label = "家庭电话",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "LABEL",
                        Label = "标签",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "LAST_LOGIN_DATE",
                        Label = "最后登录时间",
                        DataType = "datetime",
                    },
                    new
                    {
                        Name = "LAST_NAME",
                        Label = "姓",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "LOGIN_NAME",
                        Label = "登录名",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "LOGON_ENABLED",
                        Label = "启用登录",
                        DataType = "boolean",
                    },
                    new
                    {
                        Name = "MAIL_STOP",
                        Label = "邮件停用",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "MANAGER",
                        Label = "管理者",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "PAGER",
                        Label = "寻呼机",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "PASSWORD",
                        Label = "密码",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "PICTURE",
                        Label = "图片",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "PWD_IS_SET_ON",
                        Label = "密码设置时间",
                        DataType = "datetime",
                    },
                    new
                    {
                        Name = "STARTING_PAGE",
                        Label = "起始页",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "TELEPHONE",
                        Label = "电话",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "USER_NO",
                        Label = "用户编号",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "WORKING_DIRECTORY",
                        Label = "工作目录",
                        DataType = "string",
                    },
                };

                foreach (var prop in userProps)
                {
                    properties.Add(
                        new PROPERTY
                        {
                            ID = GenerateId(),
                            NAME = prop.Name,
                            LABEL = prop.Label,
                            DATA_TYPE = prop.DataType,
                            IS_KEYED = false,
                            SOURCE_ID = userType.ID,
                            CREATED_ON = DateTime.Now,
                            CONFIG_ID = GenerateId(),
                            CREATED_BY_ID = "00000000000000000000000000000000",
                        }
                    );
                }
            }

            // TEAM特有属性
            if (itemTypeDict.TryGetValue("TEAM", out var teamType))
            {
                var teamProps = new[]
                {
                    new
                    {
                        Name = "NAME",
                        Label = "名称",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "DESCRIPTION",
                        Label = "描述",
                        DataType = "string",
                    },
                };

                foreach (var prop in teamProps)
                {
                    properties.Add(
                        new PROPERTY
                        {
                            ID = GenerateId(),
                            NAME = prop.Name,
                            LABEL = prop.Label,
                            DATA_TYPE = prop.DataType,
                            IS_KEYED = false,
                            SOURCE_ID = teamType.ID,
                            CREATED_ON = DateTime.Now,
                            CONFIG_ID = GenerateId(),
                            CREATED_BY_ID = "00000000000000000000000000000000",
                        }
                    );
                }
            }

            // IDENTITY特有属性
            if (itemTypeDict.TryGetValue("IDENTITY", out var identityType))
            {
                var identityProps = new[]
                {
                    new
                    {
                        Name = "DESCRIPTION",
                        Label = "描述",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "IS_ALIAS",
                        Label = "是别名",
                        DataType = "boolean",
                    },
                    new
                    {
                        Name = "NAME",
                        Label = "名称",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "MAXIMUM_PWD_AGE",
                        Label = "最大密码年龄",
                        DataType = "integer",
                    },
                    new
                    {
                        Name = "PWD_HISTORY_LENGTH",
                        Label = "密码历史长度",
                        DataType = "integer",
                    },
                };

                foreach (var prop in identityProps)
                {
                    properties.Add(
                        new PROPERTY
                        {
                            ID = GenerateId(),
                            NAME = prop.Name,
                            LABEL = prop.Label,
                            DATA_TYPE = prop.DataType,
                            IS_KEYED = false,
                            SOURCE_ID = identityType.ID,
                            CREATED_ON = DateTime.Now,
                            CONFIG_ID = GenerateId(),
                            CREATED_BY_ID = "00000000000000000000000000000000",
                        }
                    );
                }
            }

            // TEAM_IDENTITY特有属性
            if (itemTypeDict.TryGetValue("TEAM_IDENTITY", out var teamIdentityType))
            {
                properties.Add(
                    new PROPERTY
                    {
                        ID = GenerateId(),
                        NAME = "TEAM_ROLE",
                        LABEL = "团队角色",
                        DATA_TYPE = "string",
                        IS_KEYED = false,
                        SOURCE_ID = teamIdentityType.ID,
                        CREATED_ON = DateTime.Now,
                        CONFIG_ID = GenerateId(),
                        CREATED_BY_ID = "00000000000000000000000000000000",
                    }
                );
            }

            // MEMBER特有属性
            if (itemTypeDict.TryGetValue("MEMBER", out var memberType))
            {
                var memberProps = new[]
                {
                    new
                    {
                        Name = "BEHAVIOR",
                        Label = "行为",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "END_DATE",
                        Label = "结束日期",
                        DataType = "datetime",
                    },
                    new
                    {
                        Name = "FROM_DATE",
                        Label = "开始日期",
                        DataType = "datetime",
                    },
                    new
                    {
                        Name = "LABEL",
                        Label = "标签",
                        DataType = "string",
                    },
                };

                foreach (var prop in memberProps)
                {
                    properties.Add(
                        new PROPERTY
                        {
                            ID = GenerateId(),
                            NAME = prop.Name,
                            LABEL = prop.Label,
                            DATA_TYPE = prop.DataType,
                            IS_KEYED = false,
                            SOURCE_ID = memberType.ID,
                            CREATED_ON = DateTime.Now,
                            CONFIG_ID = GenerateId(),
                            CREATED_BY_ID = "00000000000000000000000000000000",
                        }
                    );
                }
            }

            // ALIAS特有属性
            if (itemTypeDict.TryGetValue("ALIAS", out var aliasType))
            {
                var aliasProps = new[]
                {
                    new
                    {
                        Name = "BEHAVIOR",
                        Label = "行为",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "END_DATE",
                        Label = "结束日期",
                        DataType = "datetime",
                    },
                    new
                    {
                        Name = "FROM_DATE",
                        Label = "开始日期",
                        DataType = "datetime",
                    },
                    new
                    {
                        Name = "LABEL",
                        Label = "标签",
                        DataType = "string",
                    },
                };

                foreach (var prop in aliasProps)
                {
                    properties.Add(
                        new PROPERTY
                        {
                            ID = GenerateId(),
                            NAME = prop.Name,
                            LABEL = prop.Label,
                            DATA_TYPE = prop.DataType,
                            IS_KEYED = false,
                            SOURCE_ID = aliasType.ID,
                            CREATED_ON = DateTime.Now,
                            CONFIG_ID = GenerateId(),
                            CREATED_BY_ID = "00000000000000000000000000000000",
                        }
                    );
                }
            }

            // ACCESS特有属性
            if (itemTypeDict.TryGetValue("ACCESS", out var accessType))
            {
                var accessProps = new[]
                {
                    new
                    {
                        Name = "BEHAVIOR",
                        Label = "行为",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "CAN_CHANGE_ACCESS",
                        Label = "可更改访问",
                        DataType = "boolean",
                    },
                    new
                    {
                        Name = "CAN_DELETE",
                        Label = "可删除",
                        DataType = "boolean",
                    },
                    new
                    {
                        Name = "CAN_DISCOVER",
                        Label = "可发现",
                        DataType = "boolean",
                    },
                    new
                    {
                        Name = "CAN_GET",
                        Label = "可获取",
                        DataType = "boolean",
                    },
                    new
                    {
                        Name = "CAN_UPDATE",
                        Label = "可更新",
                        DataType = "boolean",
                    },
                    new
                    {
                        Name = "END_DATE",
                        Label = "结束日期",
                        DataType = "datetime",
                    },
                    new
                    {
                        Name = "FROM_DATE",
                        Label = "开始日期",
                        DataType = "datetime",
                    },
                    new
                    {
                        Name = "SHOW_PERMISSIONS_WARNING",
                        Label = "显示权限警告",
                        DataType = "boolean",
                    },
                };

                foreach (var prop in accessProps)
                {
                    properties.Add(
                        new PROPERTY
                        {
                            ID = GenerateId(),
                            NAME = prop.Name,
                            LABEL = prop.Label,
                            DATA_TYPE = prop.DataType,
                            IS_KEYED = false,
                            SOURCE_ID = accessType.ID,
                            CREATED_ON = DateTime.Now,
                            CONFIG_ID = GenerateId(),
                            CREATED_BY_ID = "00000000000000000000000000000000",
                        }
                    );
                }
            }

            // DOCUMENT_FILE特有属性
            if (itemTypeDict.TryGetValue("DOCUMENT_FILE", out var documentFileType))
            {
                var documentFileProps = new[]
                {
                    new
                    {
                        Name = "BEHAVIOR",
                        Label = "行为",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "EXTERNAL_ID",
                        Label = "外部ID",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "EXTERNAL_OWNER",
                        Label = "外部所有者",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "EXTERNAL_TYPE",
                        Label = "外部类型",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "IS_SHARED",
                        Label = "是否共享",
                        DataType = "boolean",
                    },
                };

                foreach (var prop in documentFileProps)
                {
                    properties.Add(
                        new PROPERTY
                        {
                            ID = GenerateId(),
                            NAME = prop.Name,
                            LABEL = prop.Label,
                            DATA_TYPE = prop.DataType,
                            IS_KEYED = false,
                            SOURCE_ID = documentFileType.ID,
                            CREATED_ON = DateTime.Now,
                            CONFIG_ID = GenerateId(),
                            CREATED_BY_ID = "00000000000000000000000000000000",
                        }
                    );
                }
            }

            // SERVER_EVENT特有属性
            if (itemTypeDict.TryGetValue("SERVER_EVENT", out var serverEventType))
            {
                var serverEventProps = new[]
                {
                    new
                    {
                        Name = "BEHAVIOR",
                        Label = "行为",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "EVENT_VERSION",
                        Label = "事件版本",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "IS_REQUIRED",
                        Label = "是否必需",
                        DataType = "boolean",
                    },
                    new
                    {
                        Name = "LABEL",
                        Label = "标签",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "EVENT_TYPE",
                        Label = "事件类型",
                        DataType = "string",
                    },
                };

                foreach (var prop in serverEventProps)
                {
                    properties.Add(
                        new PROPERTY
                        {
                            ID = GenerateId(),
                            NAME = prop.Name,
                            LABEL = prop.Label,
                            DATA_TYPE = prop.DataType,
                            IS_KEYED = false,
                            SOURCE_ID = serverEventType.ID,
                            CREATED_ON = DateTime.Now,
                            CONFIG_ID = GenerateId(),
                            CREATED_BY_ID = "00000000000000000000000000000000",
                        }
                    );
                }
            }

            // PERMISSION特有属性
            if (itemTypeDict.TryGetValue("PERMISSION", out var permissionType))
            {
                var permissionProps = new[]
                {
                    new
                    {
                        Name = "IS_PRIVATE",
                        Label = "是否私有",
                        DataType = "boolean",
                    },
                    new
                    {
                        Name = "NAME",
                        Label = "名称",
                        DataType = "string",
                    },
                };

                foreach (var prop in permissionProps)
                {
                    properties.Add(
                        new PROPERTY
                        {
                            ID = GenerateId(),
                            NAME = prop.Name,
                            LABEL = prop.Label,
                            DATA_TYPE = prop.DataType,
                            IS_KEYED = false,
                            SOURCE_ID = permissionType.ID,
                            CREATED_ON = DateTime.Now,
                            CONFIG_ID = GenerateId(),
                            CREATED_BY_ID = "00000000000000000000000000000000",
                        }
                    );
                }
            }

            // DOCUMENT特有属性
            if (itemTypeDict.TryGetValue("DOCUMENT", out var documentType))
            {
                var documentProps = new[]
                {
                    new
                    {
                        Name = "ARAS_UNIQUENESS_HELPER",
                        Label = "ARAS唯一性助手",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "AUTHORING_TOOL",
                        Label = "创作工具",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "AUTHORING_TOOL_VERSION",
                        Label = "创作工具版本",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "DESCRIPTION",
                        Label = "描述",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "DRAWING_SIZE",
                        Label = "图纸尺寸",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "EFFECTIVE_DATE",
                        Label = "生效日期",
                        DataType = "datetime",
                    },
                    new
                    {
                        Name = "EXTERNAL_ID",
                        Label = "外部ID",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "EXTERNAL_OWNER",
                        Label = "外部所有者",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "EXTERNAL_TYPE",
                        Label = "外部类型",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "FROM_TEMPLATE",
                        Label = "来自模板",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "NAME",
                        Label = "名称",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "SCALE",
                        Label = "比例",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "SPECIFICATION_TYPE",
                        Label = "规范类型",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "RELEASE_DATE",
                        Label = "发布日期",
                        DataType = "datetime",
                    },
                    new
                    {
                        Name = "SUPERSEDED_DATE",
                        Label = "替代日期",
                        DataType = "datetime",
                    },
                    new
                    {
                        Name = "THUMBNAIL",
                        Label = "缩略图",
                        DataType = "string",
                    },
                };

                foreach (var prop in documentProps)
                {
                    properties.Add(
                        new PROPERTY
                        {
                            ID = GenerateId(),
                            NAME = prop.Name,
                            LABEL = prop.Label,
                            DATA_TYPE = prop.DataType,
                            IS_KEYED = false,
                            SOURCE_ID = documentType.ID,
                            CREATED_ON = DateTime.Now,
                            CONFIG_ID = GenerateId(),
                            CREATED_BY_ID = "00000000000000000000000000000000",
                        }
                    );
                }
            }

            // FILE特有属性
            if (itemTypeDict.TryGetValue("FILE", out var fileType))
            {
                var fileProps = new[]
                {
                    new
                    {
                        Name = "CHECKEDOUT_PATH",
                        Label = "检出路径",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "CHECKSUM",
                        Label = "校验和",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "FILE_SIZE",
                        Label = "文件大小",
                        DataType = "decimal",
                    },
                    new
                    {
                        Name = "FILE_TYPE",
                        Label = "文件类型",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "FILENAME",
                        Label = "文件名",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "LABEL",
                        Label = "标签",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "MIMETYPE",
                        Label = "MIME类型",
                        DataType = "string",
                    },
                };

                foreach (var prop in fileProps)
                {
                    properties.Add(
                        new PROPERTY
                        {
                            ID = GenerateId(),
                            NAME = prop.Name,
                            LABEL = prop.Label,
                            DATA_TYPE = prop.DataType,
                            IS_KEYED = false,
                            SOURCE_ID = fileType.ID,
                            CREATED_ON = DateTime.Now,
                            CONFIG_ID = GenerateId(),
                            CREATED_BY_ID = "00000000000000000000000000000000",
                        }
                    );
                }
            }

            // METHOD特有属性
            if (itemTypeDict.TryGetValue("METHOD", out var methodType))
            {
                var methodProps = new[]
                {
                    new
                    {
                        Name = "ARAS_UNIQUENESS_HELPER",
                        Label = "ARAS唯一性助手",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "COMMENTS",
                        Label = "注释",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "CORE",
                        Label = "核心",
                        DataType = "boolean",
                    },
                    new
                    {
                        Name = "EXECUTION_ALLOWED_TO",
                        Label = "执行允许给",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "EFFECTIVE_DATE",
                        Label = "生效日期",
                        DataType = "datetime",
                    },
                    new
                    {
                        Name = "METHOD_CODE",
                        Label = "方法代码",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "METHOD_LOCATION",
                        Label = "方法位置",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "METHOD_TYPE",
                        Label = "方法类型",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "NAME",
                        Label = "名称",
                        DataType = "string",
                    },
                    new
                    {
                        Name = "RELEASE_DATE",
                        Label = "发布日期",
                        DataType = "datetime",
                    },
                    new
                    {
                        Name = "SUPERSEDED_DATE",
                        Label = "替代日期",
                        DataType = "datetime",
                    },
                    new
                    {
                        Name = "TEMPLATE",
                        Label = "模板",
                        DataType = "string",
                    },
                };

                foreach (var prop in methodProps)
                {
                    properties.Add(
                        new PROPERTY
                        {
                            ID = GenerateId(),
                            NAME = prop.Name,
                            LABEL = prop.Label,
                            DATA_TYPE = prop.DataType,
                            IS_KEYED = false,
                            SOURCE_ID = methodType.ID,
                            CREATED_ON = DateTime.Now,
                            CONFIG_ID = GenerateId(),
                            CREATED_BY_ID = "00000000000000000000000000000000",
                        }
                    );
                }
            }
        }

        private List<RELATIONSHIPTYPE> CreateRelationshipTypes(
            Dictionary<string, ITEMTYPE> itemTypeDict
        )
        {
            var relationshipTypes = new List<RELATIONSHIPTYPE>();

            // TEAM_IDENTITY: Source=TEAM, Related=IDENTITY
            if (
                itemTypeDict.TryGetValue("TEAM_IDENTITY", out var teamIdentityType)
                && itemTypeDict.TryGetValue("TEAM", out var teamType)
                && itemTypeDict.TryGetValue("IDENTITY", out var identityType)
            )
            {
                relationshipTypes.Add(
                    new RELATIONSHIPTYPE
                    {
                        ID = GenerateId(),
                        NAME = "TEAM_IDENTITY",
                        LABEL = "团队身份",
                        SOURCE_ID = teamType.ID,
                        RELATED_ID = identityType.ID,
                        RELATIONSHIP_ID = teamIdentityType.ID,
                        AUTO_SEARCH = true,
                        INC_REL_KEY_NAME = true,
                        INC_RELATED_KEY_NAME = true,
                        RELATED_OPTION = "2",
                        CREATED_ON = DateTime.Now,
                        CONFIG_ID = GenerateId(),
                        CREATED_BY_ID = "00000000000000000000000000000000",
                    }
                );
            }

            // MEMBER: Source=IDENTITY, Related=IDENTITY
            if (
                itemTypeDict.TryGetValue("MEMBER", out var memberType)
                && itemTypeDict.TryGetValue("IDENTITY", out var sourceIdentityType)
            )
            {
                relationshipTypes.Add(
                    new RELATIONSHIPTYPE
                    {
                        ID = GenerateId(),
                        NAME = "MEMBER",
                        LABEL = "成员",
                        SOURCE_ID = sourceIdentityType.ID,
                        RELATED_ID = sourceIdentityType.ID,
                        RELATIONSHIP_ID = memberType.ID,
                        AUTO_SEARCH = true,
                        INC_REL_KEY_NAME = true,
                        INC_RELATED_KEY_NAME = true,
                        RELATED_OPTION = "2",
                        CREATED_ON = DateTime.Now,
                        CONFIG_ID = GenerateId(),
                        CREATED_BY_ID = "00000000000000000000000000000000",
                    }
                );
            }

            // ALIAS: Source=USER, Related=IDENTITY
            if (
                itemTypeDict.TryGetValue("ALIAS", out var aliasType)
                && itemTypeDict.TryGetValue("USER", out var userType)
                && itemTypeDict.TryGetValue("IDENTITY", out var relatedIdentityType)
            )
            {
                relationshipTypes.Add(
                    new RELATIONSHIPTYPE
                    {
                        ID = GenerateId(),
                        NAME = "ALIAS",
                        LABEL = "别名",
                        SOURCE_ID = userType.ID,
                        RELATED_ID = relatedIdentityType.ID,
                        RELATIONSHIP_ID = aliasType.ID,
                        AUTO_SEARCH = true,
                        INC_REL_KEY_NAME = true,
                        INC_RELATED_KEY_NAME = true,
                        RELATED_OPTION = "2",
                        CREATED_ON = DateTime.Now,
                        CONFIG_ID = GenerateId(),
                        CREATED_BY_ID = "00000000000000000000000000000000",
                    }
                );
            }

            // ACCESS: Source=PERMISSION, Related=IDENTITY
            if (
                itemTypeDict.TryGetValue("ACCESS", out var accessType)
                && itemTypeDict.TryGetValue("PERMISSION", out var permissionType)
                && itemTypeDict.TryGetValue("IDENTITY", out var accessIdentityType)
            )
            {
                relationshipTypes.Add(
                    new RELATIONSHIPTYPE
                    {
                        ID = GenerateId(),
                        NAME = "ACCESS",
                        LABEL = "访问权限",
                        SOURCE_ID = permissionType.ID,
                        RELATED_ID = accessIdentityType.ID,
                        RELATIONSHIP_ID = accessType.ID,
                        AUTO_SEARCH = true,
                        INC_REL_KEY_NAME = true,
                        INC_RELATED_KEY_NAME = true,
                        RELATED_OPTION = "2",
                        CREATED_ON = DateTime.Now,
                        CONFIG_ID = GenerateId(),
                        CREATED_BY_ID = "00000000000000000000000000000000",
                    }
                );
            }

            // DOCUMENT_FILE: Source=DOCUMENT, Related=FILE
            if (
                itemTypeDict.TryGetValue("DOCUMENT_FILE", out var documentFileType)
                && itemTypeDict.TryGetValue("DOCUMENT", out var documentType)
                && itemTypeDict.TryGetValue("FILE", out var fileType)
            )
            {
                relationshipTypes.Add(
                    new RELATIONSHIPTYPE
                    {
                        ID = GenerateId(),
                        NAME = "DOCUMENT_FILE",
                        LABEL = "文档文件",
                        SOURCE_ID = documentType.ID,
                        RELATED_ID = fileType.ID,
                        RELATIONSHIP_ID = documentFileType.ID,
                        AUTO_SEARCH = true,
                        INC_REL_KEY_NAME = true,
                        INC_RELATED_KEY_NAME = true,
                        RELATED_OPTION = "2",
                        CREATED_ON = DateTime.Now,
                        CONFIG_ID = GenerateId(),
                        CREATED_BY_ID = "00000000000000000000000000000000",
                    }
                );
            }

            // SERVER_EVENT: Source=ITEMTYPE, Related=METHOD
            if (
                itemTypeDict.TryGetValue("SERVER_EVENT", out var serverEventType)
                && itemTypeDict.TryGetValue("ITEMTYPE", out var itemtypeType)
                && itemTypeDict.TryGetValue("METHOD", out var methodType)
            )
            {
                relationshipTypes.Add(
                    new RELATIONSHIPTYPE
                    {
                        ID = GenerateId(),
                        NAME = "SERVER_EVENT",
                        LABEL = "服务器事件",
                        SOURCE_ID = itemtypeType.ID,
                        RELATED_ID = methodType.ID,
                        RELATIONSHIP_ID = serverEventType.ID,
                        AUTO_SEARCH = true,
                        INC_REL_KEY_NAME = true,
                        INC_RELATED_KEY_NAME = true,
                        RELATED_OPTION = "2",
                        CREATED_ON = DateTime.Now,
                        CONFIG_ID = GenerateId(),
                        CREATED_BY_ID = "00000000000000000000000000000000",
                    }
                );
            }

            return relationshipTypes;
        }

        private string GenerateId()
        {
            return Guid.NewGuid().ToString("N");
        }

        /// <summary>
        /// 创建特殊身份记录
        /// </summary>
        private List<IDENTITY> CreateSpecialIdentities()
        {
            return new List<IDENTITY>
            {
                new IDENTITY
                {
                    ID = "WORLD_IDENTITY_ID",
                    NAME = "World",
                    DESCRIPTION = "所有人身份，具有基础访问权限",
                    IS_ALIAS = false,
                    MAXIMUM_PWD_AGE = 0,
                    PWD_HISTORY_LENGTH = 0,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                    OWNED_BY_ID = "00000000000000000000000000000000",
                    TEAM_ID = "00000000000000000000000000000000",
                    PERMISSION_ID = "00000000000000000000000000000000",
                    STATE = "Active",
                    IS_CURRENT = true,
                },
                new IDENTITY
                {
                    ID = "CREATOR_IDENTITY_ID",
                    NAME = "Creator",
                    DESCRIPTION = "创建者身份，具有对自己创建内容的完全权限",
                    IS_ALIAS = false,
                    MAXIMUM_PWD_AGE = 0,
                    PWD_HISTORY_LENGTH = 0,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                    OWNED_BY_ID = "00000000000000000000000000000000",
                    TEAM_ID = "00000000000000000000000000000000",
                    PERMISSION_ID = "00000000000000000000000000000000",
                    STATE = "Active",
                    IS_CURRENT = true,
                },
                new IDENTITY
                {
                    ID = "OWNER_IDENTITY_ID",
                    NAME = "Owner",
                    DESCRIPTION = "所有者身份，具有对所拥有内容的完全权限",
                    IS_ALIAS = false,
                    MAXIMUM_PWD_AGE = 0,
                    PWD_HISTORY_LENGTH = 0,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                    OWNED_BY_ID = "00000000000000000000000000000000",
                    TEAM_ID = "00000000000000000000000000000000",
                    PERMISSION_ID = "00000000000000000000000000000000",
                    STATE = "Active",
                    IS_CURRENT = true,
                },
            };
        }

        /// <summary>
        /// 创建基础权限记录
        /// </summary>
        private List<PERMISSION> CreateBasePermissions()
        {
            return new List<PERMISSION>
            {
                new PERMISSION
                {
                    ID = "DEFAULT_READ_PERMISSION",
                    NAME = "默认读取权限",
                    IS_PRIVATE = false,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                    OWNED_BY_ID = "00000000000000000000000000000000",
                    TEAM_ID = "00000000000000000000000000000000",
                    STATE = "Active",
                    IS_CURRENT = true,
                },
                new PERMISSION
                {
                    ID = "DEFAULT_FULL_PERMISSION",
                    NAME = "默认完全权限",
                    IS_PRIVATE = false,
                    CREATED_ON = DateTime.Now,
                    CONFIG_ID = GenerateId(),
                    CREATED_BY_ID = "00000000000000000000000000000000",
                    OWNED_BY_ID = "00000000000000000000000000000000",
                    TEAM_ID = "00000000000000000000000000000000",
                    STATE = "Active",
                    IS_CURRENT = true,
                },
            };
        }

        /// <summary>
        /// 创建基础访问权限记录
        /// </summary>
        private List<ACCESS> CreateBaseAccess(
            Dictionary<string, ITEMTYPE> itemTypeDict,
            List<PERMISSION> basePermissions,
            List<IDENTITY> specialIdentities
        )
        {
            var accessList = new List<ACCESS>();

            // 获取权限和身份的ID
            var readPermission = basePermissions.FirstOrDefault(p => p.NAME == "默认读取权限");
            var fullPermission = basePermissions.FirstOrDefault(p => p.NAME == "默认完全权限");
            var worldIdentity = specialIdentities.FirstOrDefault(i => i.NAME == "World");
            var creatorIdentity = specialIdentities.FirstOrDefault(i => i.NAME == "Creator");
            var ownerIdentity = specialIdentities.FirstOrDefault(i => i.NAME == "Owner");

            if (readPermission != null && worldIdentity != null)
            {
                // World身份具有读取权限
                accessList.Add(
                    new ACCESS
                    {
                        ID = GenerateId(),
                        SOURCE_ID = readPermission.ID,
                        RELATED_ID = worldIdentity.ID,
                        CAN_GET = true,
                        CAN_UPDATE = false,
                        CAN_DELETE = false,
                        CAN_DISCOVER = true,
                        CAN_CHANGE_ACCESS = false,
                        BEHAVIOR = "grant",
                        FROM_DATE = DateTime.Now,
                        END_DATE = DateTime.Now.AddYears(100),
                        SHOW_PERMISSIONS_WARNING = false,
                        CREATED_ON = DateTime.Now,
                        CONFIG_ID = GenerateId(),
                        CREATED_BY_ID = "00000000000000000000000000000000",
                        OWNED_BY_ID = "00000000000000000000000000000000",
                        TEAM_ID = "00000000000000000000000000000000",
                        STATE = "Active",
                        IS_CURRENT = true,
                    }
                );
            }

            if (fullPermission != null && creatorIdentity != null)
            {
                // Creator身份具有完全权限
                accessList.Add(
                    new ACCESS
                    {
                        ID = GenerateId(),
                        SOURCE_ID = fullPermission.ID,
                        RELATED_ID = creatorIdentity.ID,
                        CAN_GET = true,
                        CAN_UPDATE = true,
                        CAN_DELETE = true,
                        CAN_DISCOVER = true,
                        CAN_CHANGE_ACCESS = true,
                        BEHAVIOR = "grant",
                        FROM_DATE = DateTime.Now,
                        END_DATE = DateTime.Now.AddYears(100),
                        SHOW_PERMISSIONS_WARNING = false,
                        CREATED_ON = DateTime.Now,
                        CONFIG_ID = GenerateId(),
                        CREATED_BY_ID = "00000000000000000000000000000000",
                        OWNED_BY_ID = "00000000000000000000000000000000",
                        TEAM_ID = "00000000000000000000000000000000",
                        STATE = "Active",
                        IS_CURRENT = true,
                    }
                );
            }

            if (fullPermission != null && ownerIdentity != null)
            {
                // Owner身份具有完全权限
                accessList.Add(
                    new ACCESS
                    {
                        ID = GenerateId(),
                        SOURCE_ID = fullPermission.ID,
                        RELATED_ID = ownerIdentity.ID,
                        CAN_GET = true,
                        CAN_UPDATE = true,
                        CAN_DELETE = true,
                        CAN_DISCOVER = true,
                        CAN_CHANGE_ACCESS = true,
                        BEHAVIOR = "grant",
                        FROM_DATE = DateTime.Now,
                        END_DATE = DateTime.Now.AddYears(100),
                        SHOW_PERMISSIONS_WARNING = false,
                        CREATED_ON = DateTime.Now,
                        CONFIG_ID = GenerateId(),
                        CREATED_BY_ID = "00000000000000000000000000000000",
                        OWNED_BY_ID = "00000000000000000000000000000000",
                        TEAM_ID = "00000000000000000000000000000000",
                        STATE = "Active",
                        IS_CURRENT = true,
                    }
                );
            }

            return accessList;
        }
    }
}
